package com.yogee.topseller.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.zxing.common.StringUtils;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.ZixunBean;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.BasicDialog1;
import com.yogee.topseller.view.FlowLayoutZixun;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */

public class ZixunAdapter extends RecyclerView.Adapter {

    private Activity context;
    private int TYPE_ITEM = 0, TYPE_BOTTOM = 1;
    private List<ZixunBean.MemberBean> beans = new ArrayList<>();

    public ZixunAdapter(Activity context) {
        this.context = context;
    }

    public void addData(List<ZixunBean.MemberBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(!TextUtils.isEmpty(beans.get(position).getLevel())){
            return TYPE_ITEM;
        }else{
            return TYPE_BOTTOM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_zixun1, parent, false);
            return new ItemHolder(view);
        }else{
            View view = LayoutInflater.from(context).inflate(R.layout.item_zixun, parent, false);
            return new SecondHolder(view);
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemHolder){
            final ZixunBean.MemberBean bean = beans.get(position);

            if(TextUtils.isEmpty(bean.getRebate()))
                ((ItemHolder) holder).tv_yongjin.setText("电联");
            else{
                if(bean.getRebate().equals("咨询"))
                    ((ItemHolder) holder).tv_yongjin.setText(bean.getRebate());
                else{
                    if(!bean.getRebate().endsWith("%"))
                        ((ItemHolder) holder).tv_yongjin.setText(bean.getRebate()+"%");
                    else
                        ((ItemHolder) holder).tv_yongjin.setText(bean.getRebate());
                }

            }

            ((ItemHolder) holder).tv_gongsi.setText(bean.getContactCompany());
            if(TextUtils.isEmpty(bean.getDetails())){

                ((ItemHolder) holder).tv_shuoming.setVisibility(View.GONE);
            }else{
               ((ItemHolder) holder).tv_shuoming.setVisibility(View.VISIBLE);
                ((ItemHolder) holder).tv_shuoming.setText(bean.getDetails());
            }

            ((ItemHolder) holder).tv_people.setText(bean.getContact()+"：");
            String phoneNum = bean.getContactNumber();
            if(!TextUtils.isEmpty(phoneNum)&&phoneNum.length()>=7){
                ((ItemHolder) holder).tv_phone_num.setText(phoneNum.substring(0,3)+"xxxx"+phoneNum.substring(7,phoneNum.length()));
            }else{
                ((ItemHolder) holder).tv_phone_num.setText(bean.getContactNumber());
            }
            ((ItemHolder) holder).tv_address.setText(bean.getAddress());
            List<String> biaoqian = null;
            if(!TextUtils.isEmpty(beans.get(position).getLevel())){
                biaoqian = Arrays.asList(beans.get(position).getLevel().split(","));
                if(biaoqian.size()>0){
                    if(biaoqian.contains("企业认证")){
                        ((ItemHolder) holder).rl_all_info.setBackgroundResource(R.drawable.zx_bg1);
                    }else{
                        ((ItemHolder) holder).rl_all_info.setBackgroundResource(R.drawable.zx_bg2);
                    }
                    if(biaoqian.size()==1){
                        ((ItemHolder) holder).ll_t1.setVisibility(View.VISIBLE);
                        ((ItemHolder) holder).ll_t2.setVisibility(View.GONE);
                        ((ItemHolder) holder).ll_t3.setVisibility(View.GONE);
                        ((ItemHolder) holder).tv_t1.setText(biaoqian.get(0));
                    }else if(biaoqian.size()==2){
                        ((ItemHolder) holder).ll_t1.setVisibility(View.VISIBLE);
                        ((ItemHolder) holder).ll_t2.setVisibility(View.VISIBLE);
                        ((ItemHolder) holder).ll_t3.setVisibility(View.GONE);
                        ((ItemHolder) holder).tv_t1.setText(biaoqian.get(0));
                        ((ItemHolder) holder).tv_t2.setText(biaoqian.get(1));
                    }else if(biaoqian.size()==3){
                        ((ItemHolder) holder).ll_t1.setVisibility(View.VISIBLE);
                        ((ItemHolder) holder).ll_t2.setVisibility(View.VISIBLE);
                        ((ItemHolder) holder).ll_t3.setVisibility(View.VISIBLE);
                        ((ItemHolder) holder).tv_t1.setText(biaoqian.get(0));
                        ((ItemHolder) holder).tv_t2.setText(biaoqian.get(1));
                        ((ItemHolder) holder).tv_t3.setText(biaoqian.get(2));
                    }
                }
            }

            ((ItemHolder) holder).rl_all_info.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                context.startActivity(new Intent(context,));
                    if(AppUtil.isFastDoubleClick(1000)){

                    }else{
                        ((ProductDetailsActivity)context).phoneCall(bean.getId(),bean.getContactNumber());
                    }

                }
            });
        }else if(holder instanceof SecondHolder){
            final ZixunBean.MemberBean bean = beans.get(position);

            if(TextUtils.isEmpty(bean.getRebate()))
                ((SecondHolder) holder).tv_yongjin.setText("电联");
            else{
                if(bean.getRebate().equals("咨询"))
                    ((SecondHolder) holder).tv_yongjin.setText(bean.getRebate());
                else
                    ((SecondHolder) holder).tv_yongjin.setText(bean.getRebate()+"%");
            }

            ((SecondHolder) holder).tv_gongsi.setText(bean.getContactCompany());
            if(TextUtils.isEmpty(bean.getDetails())){
                ((SecondHolder) holder).view_one.setVisibility(View.GONE);
                ((SecondHolder) holder).tv_shuoming.setVisibility(View.GONE);
            }else{
                ((SecondHolder) holder).view_one.setVisibility(View.VISIBLE);
                ((SecondHolder) holder).tv_shuoming.setVisibility(View.VISIBLE);
                ((SecondHolder) holder).tv_shuoming.setText(bean.getDetails());
            }

            ((SecondHolder) holder).tv_people.setText(bean.getContact()+"：");
            String phoneNum = bean.getContactNumber();
            if(!TextUtils.isEmpty(phoneNum)&&phoneNum.length()>=7){
                ((SecondHolder) holder).tv_phone_num.setText(phoneNum.substring(0,3)+"xxxx"+phoneNum.substring(7,phoneNum.length()));
            }else{
                ((SecondHolder) holder).tv_phone_num.setText(bean.getContactNumber());
            }
            ((SecondHolder) holder).tv_address.setText(bean.getAddress());


            ((SecondHolder) holder).rl_all_info.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                context.startActivity(new Intent(context,));
                    if(AppUtil.isFastDoubleClick(1000)){

                    }else{
                        ((ProductDetailsActivity)context).phoneCall(bean.getId(),bean.getContactNumber());
                    }

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_yongjin)
        TextView tv_yongjin;
        @BindView(R.id.tv_gongsi)
        TextView tv_gongsi;
        @BindView(R.id.tv_shuoming)
        TextView tv_shuoming;
        @BindView(R.id.tv_people)
        TextView tv_people;
        @BindView(R.id.tv_phone_num)
        TextView tv_phone_num;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.img_call)
        ImageView img_call;
        @BindView(R.id.ll_info)
        LinearLayout ll_info;
        @BindView(R.id.rl_all_info)
        RelativeLayout rl_all_info;
        @BindView(R.id.ll_t1)
        LinearLayout ll_t1;
        @BindView(R.id.ll_t2)
        LinearLayout ll_t2;
        @BindView(R.id.ll_t3)
        LinearLayout ll_t3;
        @BindView(R.id.tv_t1)
        TextView tv_t1;
        @BindView(R.id.tv_t2)
        TextView tv_t2;
        @BindView(R.id.tv_t3)
        TextView tv_t3;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    class SecondHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_yongjin)
        TextView tv_yongjin;
        @BindView(R.id.tv_gongsi)
        TextView tv_gongsi;
        @BindView(R.id.tv_shuoming)
        TextView tv_shuoming;
        @BindView(R.id.tv_people)
        TextView tv_people;
        @BindView(R.id.tv_phone_num)
        TextView tv_phone_num;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.img_call)
        ImageView img_call;
        @BindView(R.id.ll_info)
        LinearLayout ll_info;
        @BindView(R.id.rl_all_info)
        RelativeLayout rl_all_info;
        @BindView(R.id.view_one)
        View view_one;
        @BindView(R.id.view_two)
        View view_two;

        @BindView(R.id.flash_details_defout_fl)
        FlowLayoutZixun flash_details_defout_fl;

        public SecondHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}